#include "EWAUITrackBodyAudio.hpp"

#include "EWAPrjTrackUI.hpp"
#include "EWAColorSet.hpp"
#include "EWAPrjFragmentFragmentResourse.hpp"

namespace Eyer
{
    EWAUITrackBodyAudio::EWAUITrackBodyAudio(EyerSmartPtr<EWAUITimelineContext> _timelineCtx, EWAUILayoutParams _width, EWAUILayoutParams _height)
        : EWAUITrackBody(_timelineCtx, _width, _height)
    {
        SetWidgetName("EWAUITrackBodyAudio");
        SetLayoutType(EWAUILayoutType::FixedLayout);
    }

    EWAUITrackBodyAudio::~EWAUITrackBodyAudio()
    {

    }

    int EWAUITrackBodyAudio::OnDraw()
    {
        RemoveAllUIGraph();

        EWAPrjTrackType trackType = GetTrackType();
        int trackCount = timelineCtx->timeline.GetTrackCount(trackType);

        int offsetY = 0;

        for(int i = 0; i < trackCount; i++){
            EyerSmartPtr<EWAPrjTrackUI> track = timelineCtx->timeline.GetTrack(i, trackType);

            EyerSmartPtr<EWAUITrackHeaderItem> item = track->item;
            EyerSmartPtr<EWAUIWidgetVerticalSegBar> segBar = track->segBar;

            int itemH = item->wh.height();
            {
                EyerSmartPtr<EWAUIGraphRect> rect(new EWAUIGraphRect());
                rect->xy.SetX(0);
                rect->xy.SetY(offsetY);
                rect->wh.width(wh.width());
                rect->wh.height(itemH);
                rect->background = EWAColorSet::TRACK_BODY_BG;
                AddUIGraph(rect);
            }
            offsetY += itemH;

            int segBarH = segBar->wh.height();
            {
                EyerSmartPtr<EWAUIGraphRect> rect(new EWAUIGraphRect());
                rect->xy.SetX(0);
                rect->xy.SetY(offsetY);
                rect->wh.width(wh.width());
                rect->wh.height(segBarH);
                rect->background = EyerColor::GRAY_34;
                AddUIGraph(rect);
            }
            offsetY += segBarH;
        }

        OnDrawFragment();

        SetMinWH(2000, offsetY);
        return 0;
    }

    int EWAUITrackBodyAudio::OnDrawFragment()
    {
        EWAPrjTrackType trackType = GetTrackType();
        int trackCount = timelineCtx->timeline.GetTrackCount(trackType);

        for(int i = 0; i < trackCount; i++) {
            EyerSmartPtr<EWAPrjTrackUI> track = timelineCtx->timeline.GetTrack(i, trackType);

            EyerSmartPtr<EWAUITrackHeaderItem> item = track->item;
            EyerSmartPtr<EWAUIWidgetVerticalSegBar> segBar = track->segBar;

            // fragment
            int offsetX = 0;
            for (int j = 0; j < track->GetFragmentCount(); j++) {
                EyerSmartPtr<EWAPrjFragmentFragmentResourse> fragment = track->GetFragment(j);
                offsetX += fragment->wh.width();
            }
        }
        return 0;
    }

    bool EWAUITrackBodyAudio::OnMousePressEvent     (const Vec2I & pos)
    {
        return true;
    }

    bool EWAUITrackBodyAudio::OnMouseReleaseEvent   (const Vec2I & pos)
    {
        return true;
    }

    bool EWAUITrackBodyAudio::OnMouseMoveEvent      (const Vec2I & pos)
    {
        EyerSmartPtr<EWAPrjFragmentFragmentResourse> fragment = FindDragingFragment();
        if(fragment != nullptr){
            fragment->xy = pos - fragment->pressPos;
        }
        return true;
    }

    EyerSmartPtr<EWAPrjFragmentFragmentResourse> EWAUITrackBodyAudio::FindDragingFragment()
    {
        EyerSmartPtr<EWAPrjFragmentFragmentResourse> outFragment(nullptr);

        EWAPrjTrackType trackType = GetTrackType();
        int trackCount = timelineCtx->timeline.GetTrackCount(trackType);
        for(int i = 0; i < trackCount; i++) {
            EyerSmartPtr<EWAPrjTrackUI> track = timelineCtx->timeline.GetTrack(i, trackType);
            for (int j = 0; j < track->GetFragmentCount(); j++) {
                EyerSmartPtr<EWAPrjFragmentFragmentResourse> fragment = track->GetFragment(j);
                if(fragment->IsDraging()){
                    outFragment = fragment;
                }
            }
        }

        return outFragment;
    }

    EWAPrjTrackType EWAUITrackBodyAudio::GetTrackType()
    {
        return EWAPrjTrackType::AUDIO;
    }
}